﻿/*
*NoteBookList 0.1
*Copyright (c) 2012 AtHome
*Date:2012-10-22
*/
(function ($) {
    $.bookAreaPage32 = function (options) {
        var defaults = {
            noteType: "notebook"
        }
        var options = $.extend(defaults, options);

        //--------------笔记本列表
        if (options.noteType == "notebook") {
            var data = $.Ajax32.bookAreaPage1();
            $.post("/NoteBook/GetNoteBookListAndAreaList", {}, function (data, status) {
                if (status != "success") {
                    alert("获取笔记本列表异常，请刷新页面！");
                    return;
                }
                //--------------笔记本
                var JQbookList = $("#bookList");
                var bookList = data.NoteBookList; //笔记本集合
                var bookSort = data.NoteBookSort.split('-'); //笔记本排序
                JQbookList.empty();
                for (var i in bookSort) {
                    for (var j = 0; j < bookList.length; j++) {
                        if (bookSort[i] == bookList[j].ID) {
                            var JQbook = $('<div class="book"><div class="head"><span class="title"></span><div class="arrow"></div></div><div class="areaList"></div></div>').appendTo(JQbookList);
                            JQbook.attr("id", "book_" + bookList[j].ID);
                            JQbook.find(".head .title").text(bookList[j].Name);
                        }
                    }
                }


                //--------------分区

                var areaList = data.AreaList; //所有分区集合
                for (var i = 0; i < bookList.length; i++) {
                    JQbookList.find(".book").each(function () {
                        if ($(this).idAnaly_({ toward: "head" }) == "book" && $(this).idAnaly_() == bookList[i].ID) {
                            var JQbook = $(this); //对应的笔记本元素
                            var areaList2 = new Array(); //对应的分区集合
                            var areaSort = bookList[i].AreaSort.split('-'); //对应的分区排序
                            for (var j = 0; j < areaList.length; j++) {
                                if (areaList[j].NoteBookID == JQbook.idAnaly_()) {
                                    areaList2.push(areaList[j]);
                                }
                            }
                            for (var j in areaSort) {
                                for (var z = 0; z < areaList2.length; z++) {
                                    if (areaSort[j] == areaList2[z].ID) {
                                        var JQareaList = JQbook.find(".areaList");
                                        var JQarea = $('<div class="area"><div class="icon"></div><span class="title"></span></div>');
                                        JQarea.appendTo(JQareaList);
                                        JQarea.attr("id", "area_" + areaList2[z].ID + "_" + bookList[i].ID);
                                        JQarea.find(".title").text(areaList2[z].Name);
                                    }
                                }
                            }
                        }
                    });
                }


                //--------------页
                var JQpageList = $("#pageList");
                var pageList = data.PageList; //页集合
                var pageSort = data.AreaList[0].PageSort.split('-'); //页排序
                JQpageList.empty();
                for (var i in pageSort) {
                    for (var j = 0; j < pageList.length; j++) {
                        if (pageSort[i] == pageList[j].ID) {
                            var JQpage = $('<div class="page"><div class="bg"></div><span class="title"></span><span class="date"></span><div class="line"></div></div>').appendTo(JQpageList);
                            JQpage.attr("id", "page_" + pageList[j].ID + "_" + pageList[j].AreaID);
                            JQpage.find(".title").text(pageList[j].Name);
                            JQpage.find(".date").text($.Json_.changeTime({ data: pageList[j].CreateTime }));
                        }
                    }
                }

                $.bookArea32();
                $.area32();
                $.page32();
            });
        }
        //--------------便签列表
        else if (options.noteType == "note") {
            $.post("/NoteBook/GetNoteBookListAndAreaList", {}, function (data, status) {
                if (status != "success") {
                    alert("获取便签列表异常，请重试！");
                    return;
                }
            });
        }
    }


    $.bookArea32 = function () {
        $("#bookList").find(".book .title").followParent32({
            left: 10,
            middle: true
        });
        $("#bookList").find(".book .arrow").followParent32({
            right: 0
        });
        $("#bookList").find(".area .icon").followParent_({
            toLeft: 16,
            middle: true
        });
        $("#bookList").find(".area .title").followParent_({
            toLeft: 42,
            middle: true
        });
        //-----------------笔记本排序
        $("#bookList").sortable({
            delay: 2,
            opacity: 0.35,
            stop: function () {
                if (false) {
                    $(this).sortable('cancel');
                }
            }
        });
        //-----------------笔记本展开与收起
        $("#bookList").find(".areaList:not(:eq(0))").hide();
        $("#bookList").find(".book .arrow:eq(0)").addClass("arrow_down");
        $("#bookList").find(".book .head").click(function () {
            if ($(this).next(".areaList").html() != "") {
                $(this).find(".arrow").toggleClass("arrow_down");
                $(this).next(".areaList").toggle("fast", function () {
                    $("#bookList").find(".book .arrow").followParent32({ right: 0 });
                });
            }
        });
        //-----------------笔记本右键
        $("#bookList").find(".book .head").contextMenu("menu_notebook", {
            bindings: {
                "menu_book_Add": function (item) {
                    var JQself = $(item);
                    var JQareaList = JQself.next();
                    var JQarea = $('<div class="area"><div class="icon"></div><span class="title"></span></div>').appendTo(JQareaList);
                    JQarea.find(".icon").followParent32({ left: 16, middle: true });
                    JQarea.find(".title").followParent32({ left: 42, top: 7.5 }).text("新建分区");
                    JQarea.find(".title").rename32({
                        width: 120,
                        left: 10,
                        color: "#06f",
                        fn: function (ui32) {
                            $.post("/Area/Create", { notebookID: JQself.parent().idAnaly_(), areaName: ui32.newName }, function (data, status) {
                                if (status != "success" || data.Message != 1 || !data) {
                                    alert("新建失败，请重试！");
                                    JQarea.remove();
                                }
                                if (data.Message == 1) {
                                    ui32.rename();
                                    JQarea.attr("id", "area_" + AreaID + "_" + JQself.parent().idAnaly_());
                                }
                            });
                        }
                    });

                },
                "menu_book_ReName": function (item) {
                    var JQselft = $(item);
                    JQselft.find(".title").rename32({
                        width: 120,
                        left: 10,
                        color: "#06f",
                        fn: function (ui32) {
                            var notebookID = JQselft.parent().idAnaly_();
                            var notebookName = ui32.newName;
                            alert(notebookID + "，" + notebookName);
                            $.post("/NoteBook/Update", { notebookID: notebookID, notebookName: ui32.newName }, function (data, status) {
                                if (status != "success") {
                                    alert("重命名失败，请重试！");
                                    return;
                                }
                                if (data.Message == 1) {
                                    ui32.rename();
                                }
                            });
                        }
                    });
                },
                "menu_book_Del": function (item) {
                    var JQselft = $(item);
                    var notebookID = JQselft.parent().idAnaly_();
                    alert(notebookID);
                    $.post("/NoteBook/Delete", { notebookID: notebookID }, function (data, status) {
                        if (status != "success") {
                            alert("删除失败，请重试！");
                            return;
                        }
                        if (data.Message == 1) {
                            JQselft.parent(".book").remove();
                        }
                    });
                }
            }
        });


        //-----------------分区排序
        $("#bookList").find(".areaList").sortable({
            delay: 2, /*为防与点击事件冲突，延时2秒*/
            opacity: 0.35,
            stop: function () {
                $.bookSlider32();
            }
        });
        //-----------------分区右键
        $("#bookList").find(".area").contextMenu("menu_area", {
            bindings: {
                "menu_area_Add": function (item) {
                    var JQself = $(item);
                    var JQpageList = $("#pageList");
                    //                    if (JQself.idAnaly_() != JQpageList.find(".page").idAnaly_("father") || JQpageList.html() != "") {
                    //                        alert("请展开此分区再添加页！");
                    //                        return false;
                    //                    }

                    var JQpage = $('<div class="page"><div class="bg"></div><span class="title"></span><span class="date"></span><div class="line"></div></div>').appendTo(JQpageList);
                    JQpage.find(".title").followParent32({ left: 20, top: 4 }).text("新建页");
                    JQpage.find(".bg").followParent32({ cutHeight: 2 });
                    JQpage.find(".line").followParent32({
                        cutWidth: 26,
                        bottom: 0,
                        center: true
                    });
                    JQpage.find(".title").rename32({
                        width: 120,
                        color: "#06f",
                        fn: function (ui32) {
                            var areaID;
                            var pageName;
                            var pageContent;
                            ui32.JQhelper.followParent32({ left: 20, top: 10 });
                            $.post("/Page/Create", { areaID: areaID, pageName: pageName, pageContent: pageContent }, function (data, status) {
                                if (status != "success" || data.Message != 1 || !data) {
                                    alert("新建失败，请重试！");
                                    JQpage.remove();
                                }
                                if (data.Message == 1) {
                                    ui32.rename();
                                    JQarea.attr("id", "area_" + AreaID + "_" + JQself.parent().idAnaly_());
                                }
                            });
                        }
                    });

                },
                "menu_area_ReName": function (item) {
                    var JQselft = $(item);
                    JQselft.find(".title").rename32({
                        width: 120,
                        left: 10,
                        color: "#06f",
                        fn: function (ui32) {
                            var areaID = JQselft.idAnaly_();
                            var areaName = ui32.newName();
                            $.post("/Area/Update", { areaID: areaID, areaName: areaName }, function (data, status) {
                                if (status != "success") {
                                    alert("重命名失败，请重试！");
                                    return;
                                }
                                if (data.Message == 1) {
                                    ui32.rename();
                                }
                            });
                        }
                    });
                },
                "menu_area_Del": function (item) {
                    var JQselft = $(item);
                    var areaID = JQselft.idAnaly_()
                    $.post("/Area/Delete", { areaID: areaID }, function (data, status) {
                        if (status != "success") {
                            alert("删除失败，请重试！");
                            return;
                        }
                        if (data.Message == 1) {
                            JQselft.remove();
                        }
                    });
                }
            }
        });

        //-----------------点击分区刷新页信息
        $.area32 = function () {
            $("#bookList").find(".areaList .area").click(function () {
                var JQselft = $(this);
                var areaID = JQselft.idAnaly_();
                var pageIndex = "1";
                var pageSize = "50";
                $.post("/Page/GetAll", { areaID: areaID, pageIndex: pageIndex, pageSize: pageSize, SortType: null }, function (data, status) {
                    if (status != "success") {
                        alert("网络异常，请重试！");
                        return;
                    }
                    if (data.Message == 1) {

                        var JQpageList = $("#pageList");
                        var pageList = data.PageList; //页集合
                        JQpageList.empty();
                        for (var i = 0; i < pageList.length; i++) {
                            var JQpage = $('<div class="page"><div class="bg"></div><span class="title"></span><span class="date"></span><div class="line"></div></div>').appendTo(JQpageList);
                            JQpage.attr("id", "page_" + pageList[i].ID + "_" + pageList[i].AreaID);
                            JQpage.find(".title").text(pageList[i].Name);
                            JQpage.find(".date").text($.Json_.changeTime({ data: pageList[i].CreateTime }));
                        }

                        $.page32();
                    }
                });
            });
        }

        //-----------------鼠标经过
        $("#bookList").find(".head").hover(function () {
            $(this).addClass("head_over");
            $(this).find(".arrow").addClass("arrow_over");
        }, function () {
            $(this).removeClass("head_over");
            $(this).find(".arrow").removeClass("arrow_over");
        });

        //-----------------bookList创建滚动条
        //$("#bookList").jScrollPane({ scrollbarWidth: 10 });
        $("#bookList").followParent32({
            cutWidth: 4,
            top: 30
        });
        //        $("#bookList").find(".jspContainer").width($("#bookList").width());
        //        $("#bookList").find(".jspPane").width($("#bookList").width() - 12);
        $("#bookList").find(".book .head .arrow").followParent32({
            right: 0
        });

        //-----------------pageList创建滚动条
        //$("#pageList").jScrollPane();

    }


    $.page32 = function () {
        $("#pageList").find(".title").followParent32({
            top: 4,
            left: 20
        });
        $("#pageList").find(".date").followParent32({
            top: 4,
            right: 20
        });
        $("#pageList").find(".bg").followParent32({
            cutHeight: 2
        }).buttonActive_({
            over: "bg_over",
            down: "bg_down",
            click: function () {
                JQself.find(".bg").removeClass("bg_click");
                $(this).addClass("bg_click");
            }
        });
        $("#pageList").find(".line").followParent32({
            cutWidth: 26,
            bottom: 0,
            center: true
        });

        //-----------------region创建滚动条
        $("#regionOuter").jScrollPane();

        //-----------------页排序
        $("#pageList").sortable({
            delay: 2, /*为防与点击事件冲突，延时2秒*/
            opacity: 0.35,
            stop: function () {
                var JQitem = $(this);
                $.post("", {}, function () {
                    if (status != "success") {
                        JQitem.sortable("cancel");
                        alert("排序失败，请重试！");
                        return;
                    }
                });
            }
        });

        //-----------------页右键
        $("#pageList").find(".page").contextMenu("menu_page", {
            bindings: {
                "menu_page_Add": function (item) {
                    var JQitem = $(item);
                    $.post("", {}, function () {
                        if (status != "success") {
                            alert("新建失败，请重试！");
                            return;
                        }
                    });
                },
                "menu_page_ReName": function (item) {
                    var JQitem = $(item);
                    $.post("", {}, function () {
                        if (status != "success") {
                            alert("重命名失败，请重试！");
                            return;
                        }
                    });
                },
                "menu_page_Del": function (item) {
                    var JQitem = $(item);
                    $.post("", {}, function () {
                        if (status != "success") {
                            alert("删除失败，请重试！");
                            return;
                        }
                        JQitem.remove();
                    });
                }
            }
        });
    }


    $.bookSlider32 = function () {
        $("#bookList").data("jsp").reinitialise();
        //$("#bookList").find(".jspVerticalBar").width(10);
        //        $("#bookList").find(".book .head .arrow").followParent32({
        //            right: 0
        //        });
        //        $("#bookList").followParent32({
        //            cutWidth: 4
        //        });
    }


    $.fn.plateBook32 = {
        facade: function (options) {
            var defaults = {};
            var options = $.extend(defaults, options);
        },

        action: function () {

        }
    }

    //重命名插件
    $.fn.rename32 = function (options) {
        var defaults = {
            width: 100,
            height: 20,
            left: 8,
            fontSize: 12,
            color: "#000",
            fn: function () { }
        };
        var ui32 = {
            newName: "",
            rename: function () { }
        };
        var options = $.extend({}, defaults, options);
        return this.each(function () {
            ui32.JQhelper = JQtextbox;
            var JQself = $(this);
            JQself.hide();
            var JQtextbox = $('<input class="textbox" type="text" />').appendTo(JQself.parent());
            JQtextbox.val(JQself.text()).followParent32({
                middle: true,
                left: options.left
            }).width(options.width).height(options.height).css({
                "text-indent": 4,
                "font-size": options.fontSize,
                "color": options.color
            }).focus();

            ui32.rename = function () {
                JQself.text(JQtextbox.val());
                JQtextbox.remove();
            }


            JQtextbox.focusout(function () {
                JQtextbox.hide();
                JQself.show();
                ui32.newName = JQtextbox.val();
                if (JQtextbox.val() == JQself.text()) {
                    return false;
                }
                if (JQtextbox.val() == "") {
                    alert("命名不可为空！");
                    return false;
                }
                options.fn(ui32); //当文本框失去焦点后执行用户定义的方法
            });
        });
    }

})(jQuery);
